//
//  MyGoodsViewController.m
//  jingdongfang
//
//  Created by MTiMac02 on 2018/1/3.
//  Copyright © 2018年 LJL. All rights reserved.
//

#import "MyGoodsViewController.h"
#import "SliderViewController.h"
#import "AllGoodsVC.h"
#import "CheckingGoodsVC.h"
#import "SaleGoodsVC.h"
#import "SoldOutVC.h"
#import "NoPassGoodsVC.h"
#import "MyGoodsTitleButton.h"

@interface MyGoodsViewController ()<UIScrollViewDelegate>
{
    AllGoodsVC *vc1;
    CheckingGoodsVC *vc2;
    SaleGoodsVC *vc3;
    SoldOutVC *vc4;
    NoPassGoodsVC *vc5;
}
@property(nonatomic,weak)UIScrollView *scrollView;
@property(nonatomic,weak)UIView * titleView;
//记录上一次点击的按钮
@property(nonatomic,weak)MyGoodsTitleButton *preClickTitleBtn;
@property(nonatomic,weak)UIView *titleLine;
@property (nonatomic, strong)  SliderViewController *sliderVC;
@end

@implementation MyGoodsViewController
@synthesize sliderVC;
#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = NSLocalizedString(@"My Item(s)", nil);
    [self setupVC];
    [self setupScrollView];
    [self setupTitlesView];
}
-(void)setupVC
{
    [self addChildViewController:[[AllGoodsVC alloc]init]];
    [self addChildViewController:[[CheckingGoodsVC alloc]init]];
    [self addChildViewController:[[SaleGoodsVC alloc]init]];
    [self addChildViewController:[[SoldOutVC alloc]init]];
    [self addChildViewController:[[NoPassGoodsVC alloc]init]];
    
}
-(void)setupScrollView
{
    self.automaticallyAdjustsScrollViewInsets=NO;
    UIScrollView *scrollView=[[UIScrollView alloc]init];
    scrollView.backgroundColor=RGB(234, 234, 234);
    scrollView.frame=self.view.bounds;
    scrollView.pagingEnabled=YES;
    scrollView.delegate=self;
    scrollView.showsVerticalScrollIndicator=NO;
    scrollView.showsHorizontalScrollIndicator=NO;
    scrollView.scrollsToTop=NO;
    [self.view addSubview:scrollView];
    self.scrollView=scrollView;
    NSUInteger count=self.childViewControllers.count;
    CGFloat scrollViewW=scrollView.width;
//    CGFloat scrllViewH=scrollView.height;
//    for (NSUInteger i=0; i<count; i++) {
//        UIView *childView=self.childViewControllers[i].view;
//        childView.x=i*scrollViewW;
//        childView.frame=CGRectMake(i*scrollViewW, NavigationMagin+41, scrollViewW, scrllViewH+105);
//        [scrollView addSubview:childView];
//    }
    scrollView.contentSize=CGSizeMake(count*scrollViewW, 0);
}
-(void)setupTitlesView
{
    UIView *titleView=[[UIView alloc]init];
    titleView.backgroundColor=[UIColor c3Color];
   titleView.frame=CGRectMake(0, 65, KSCREENWIDTH, 40);
    self.titleView=titleView;
    [self.view addSubview:titleView];
    [self setupTitleButtons];
    [self setupTitleLines];
}
-(void)setupTitleButtons
{
    //文字
    NSArray *titles=@[@"全部",@"待审核",@"已上架",@"已下架",@"审核不通过"];
    NSUInteger count=titles.count;
    //标题按妞尺寸
    CGFloat titleButtonW=KSCREENWIDTH/5;
    CGFloat titleButtonH=self.titleView.height;
    //创建5个标题按钮
    for (NSUInteger i=0; i<count; i++) {
        MyGoodsTitleButton *titleButton=[[MyGoodsTitleButton alloc]init];
        titleButton.tag=i;
        [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleView addSubview:titleButton];
         titleButton.frame=CGRectMake(i*titleButtonW, 0, titleButtonW, titleButtonH);
        if (i==0) {
             titleButton.frame=CGRectMake(i*titleButtonW, 0, titleButtonW-20, titleButtonH);
        }
        if (i==1) {
            titleButton.frame=CGRectMake(titleButtonW-20+(i-1)*titleButtonW, 0, titleButtonW, titleButtonH);
        }
        if (i==2) {
            titleButton.frame=CGRectMake(titleButtonW-20+(i-1)*titleButtonW, 0, titleButtonW, titleButtonH);
        }
        if (i==3) {
            titleButton.frame=CGRectMake(titleButtonW-20+(i-1)*titleButtonW, 0, titleButtonW, titleButtonH);
        }
        if (i==4) {
            titleButton.frame=CGRectMake(titleButtonW-20+(i-1)*titleButtonW, 0, titleButtonW+20, titleButtonH);
        }
       [titleButton setTitle:titles[i] forState:UIControlStateNormal];
    }
}
-(void)setupTitleLines
{
    MyGoodsTitleButton *firstTitleBtn=self.titleView.subviews.firstObject;
    UIView *titleLine=[[UIView alloc]init];
    titleLine.height=2;
    titleLine.y=self.titleView.height-titleLine.height;
    titleLine.backgroundColor=[firstTitleBtn titleColorForState:UIControlStateSelected];
    [self.titleView addSubview:titleLine];
    self.titleLine=titleLine;
    //默认点击第一个button
    [firstTitleBtn.titleLabel sizeToFit];
    [self titleButtonClick:firstTitleBtn];
    
}

#pragma mark-----监听
-(void)titleButtonClick:(MyGoodsTitleButton *)titleButton
{
    self.preClickTitleBtn.selected=NO;
    titleButton.selected=YES;
    self.preClickTitleBtn=titleButton;
    NSUInteger index=titleButton.tag;
    [UIView animateWithDuration:0.25 animations:^{
        self.titleLine.width=titleButton.titleLabel.width+10;
        self.titleLine.centerX=titleButton.centerX;
        CGFloat offsetX=self.scrollView.width*titleButton.tag;
        self.scrollView.contentOffset=CGPointMake(offsetX, self.scrollView.contentOffset.y);
    }completion:^(BOOL finished) {
        UIView *childView=self.childViewControllers[index].view;
        if (childView.superview) {
            return ;
        }
        childView.frame=CGRectMake(index*self.scrollView.width, NavigationMagin+41, self.scrollView.width, self.scrollView.height+NavigationMagin+41);
        [self.scrollView addSubview:childView];
    }];
    for (NSUInteger i=0; i<self.childViewControllers.count; i++) {
        UIViewController *childVc=self.childViewControllers[i];
        if (!childVc.isViewLoaded)continue;
        UIScrollView *scrollView=(UIScrollView *)childVc.view;
        if (![scrollView isKindOfClass:[UIScrollView class]]) continue;
        scrollView.scrollsToTop=(i==index);
    }
    
}
- (void)setIndex:(NSInteger)index {
    if (_index != index) {
        _index = index;
    }
    if (sliderVC) {
        [sliderVC selectIndex:index];
    }
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSUInteger index=scrollView.contentOffset.x/scrollView.width;
    MyGoodsTitleButton *titleBtn=self.titleView.subviews[index];
    [self titleButtonClick:titleBtn];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
